import time

from zeus.operation_service.app.core.alarm_util.alarm_handler import get_range_alarm_info
from vulcanus.log.log import LOGGER
from vulcanus.restful.response import BaseResponse
from vulcanus.restful.resp.state import (
    SUCCEED,
    UNKNOWN_ERROR,
)

HALF_HOUR = 1800000
TEN_MINUTE = 600000


class AlarmManageAPI(BaseResponse):
    @BaseResponse.handle(token=False)
    def get(self, **params):
        try:
            # 默认展示半小时内数据
            end_time = int(params.get("endTime", int(time.time()) * 1000))
            start_time = int(params.get("startTime", end_time - HALF_HOUR))
            LOGGER.info(f"{start_time} {end_time}")
            res_data = get_range_alarm_info(start_time, end_time)
            return self.response(code=SUCCEED, data=res_data)
        except (KeyError, ValueError, Exception) as e:
            LOGGER.error(f"get alarm error: {e}")
            return self.response(code=UNKNOWN_ERROR, data={})
